package org.im.service.websocket.server.pool;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.websocket.Session;

public class WebsocketSessionPool {

	private static final Map<String, Session> SESSION_MAP = new HashMap<>(50);
	
	public static String addSession(Session session) {
		if(session != null) {
			String sessionId = session.getId();
			SESSION_MAP.put(sessionId, session);
			return sessionId;
		}
		return null;
	}
	
	public static Session removeSession(String sessionId) {
		if(sessionId == null) {
			return null;
		}
		return SESSION_MAP.remove(sessionId);
	}
	public static Session removeSession(Session session) {
		if(session == null) {
			return null;
		}
		return SESSION_MAP.remove(session.getId());
	}
	
	public static Session getSession(String sessionId) {
		if(sessionId == null) {
			return null;
		}
		return SESSION_MAP.get(sessionId);
	}
	
	public static Collection<String> getAllSessionIds() {
		Set<String> keys = SESSION_MAP.keySet();
		List<String> ids = new ArrayList<>(keys.size());
		for(String id : keys) {
			ids.add(id);
		}
		return ids;
	}
}
